<script>
	export default {
		globalData: {
			re_url:''
		},
		onLaunch: function(option) {
			console.log('App Launch')
			console.log('App Launch', option)
			var path=option.path
			var c=option.query["c"]
			var p=option.query["p"]
			console.log("c",c)
			console.log("p",p)
			console.log("path",path)
			
			var domain="http://mall.xmqiaoku.com"
			var re_url=domain+"/res/h5fx/index.html"
			
			this.globalData.re_url=re_url;
			
			if (process.env.NODE_ENV === 'development') {
			  console.log('开发环境');
			  // 示例：测试环境 API 地址
			  uni.setStorageSync("_token","xdxbawm2nd3umckrrtgm81gf")
			} else {
			  console.log('生产环境');
			}
			
			//#ifdef H5
			var token=uni.getStorageSync("_token") 
			if(token!=undefined&&token!=""){
				uni.request({
					url:domain+"/api/app/checktoken",
					header:{
						"token":token
					},
					success: (res) => {
						var rst=res.data;
						if(rst.code==204){
							uni.setStorageSync("_token","")
							//token过期，重新授权登录
							window.location =domain+"/wxmp/oauth2wid?wxid=10000&scope=snsapi_userinfo&re_url="+re_url
						}
					}
				})
			}else{
				var h5params = window.location.search.substr(1).split("&")
				if (h5params.length > 1) {
					var openid=""
					var state=""
					for(var i=0;i<h5params.length;i++){
						var h5param=h5params[i].split("=")
						if(h5param[0]=="_openid"){
							openid=h5param[1]
						}
						if(h5param[0]=="_state"){
							state=h5param[1]
						}
					}
					uni.request({
						url:domain+"/api/app/gettoken",
						data:{
							openid:openid,
							state:state,
							c:option.c
						},
						success: (res) => {
							var rst=res.data;
							if(rst.code==100){
								var token1=rst.token;
								uni.setStorageSync("_token",token1)
							}else if(rst.code==204){
								uni.setStorageSync("_token","")
								window.location =domain+"/wxmp/oauth2wid?wxid=10000&scope=snsapi_userinfo&re_url="+re_url
										// "http://mall.xmqiaoku.com/wxmp/xf/oauth2wid?wxid=10000&scope=snsapi_userinfo&re_url=http://localhost:8081"
							}else{
								console.log("获取token失败",rst)
							}
						}
					})
				}else{
					if(uni.getStorageSync("_token")){
						uni.request({
							url:domain+"/api/app/checktoken",
							header:{
								"token":uni.getStorageSync("_token")
							},
							success: (res) => {
								var rst=res.data;
								if(rst.code==204){
									uni.setStorageSync("_token","")
									//token过期，重新授权登录
									window.location =domain+"/wxmp/oauth2wid?wxid=10000&scope=snsapi_userinfo&re_url="+re_url
								}
							}
						})
					}else{
						uni.setStorageSync("_token","")
						window.location =domain+"/wxmp/oauth2wid?wxid=10000&scope=snsapi_userinfo&re_url="+re_url
					}
				}
			}
			//#endif
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	
	@import "@/common/colorui/main.css";
	@import "@/common/colorui/icon.css";
</style>